const path = require('path')
const webpack = require('webpack')
const CompressionPlugin = require('compression-webpack-plugin')
const defaultSettings = require('./src/settings.js')
function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  productionSourceMap: false,
  configureWebpack: {
    name: defaultSettings.title,
    plugins: [
      new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'windows.jQuery': 'jquery'
      })
    ],
    externals: {
      'mars3d-cesium': 'Cesium'
    }
  },
  chainWebpack: config => {
    config.resolve.alias
      .set('@$', resolve('src'))
      .set('@api', resolve('src/api'))
      .set('@assets', resolve('src/assets'))
      .set('@comp', resolve('src/components'))
      .set('@views', resolve('src/views'))
      .set('@static', resolve('src/static'))
      .set('@utils', resolve('src/utils'))

    // 生产环境打包优化
    if (process.env.NODE_ENV === 'production') {
      // 开启js\css压缩
      config.plugin('compressionPlugin').use(
        new CompressionPlugin({
          test: /\.js$|.\css|.\less/, // 匹配文件名
          threshold: 10240, // 对超过10k的数据压缩
          deleteOriginalAssets: false // 不删除源文件
        })
      )
      // 去除异常输出打印注释
      config.optimization.minimizer('terser').tap((args) => {
        args[0].terserOptions.compress.warnings = true
        args[0].terserOptions.compress.drop_console = true
        args[0].terserOptions.compress.drop_debugger = true
        args[0].terserOptions.compress.pure_funcs = ['console.log']
        args[0].terserOptions.output = {
          comments: false
        }
        return args
      })
    }
  },
  css: {
    loaderOptions: {
      sass: {
        prependData: `
          @import "@/style/variables.scss";
          @import "@/style/mixin.scss";
        `
      }
    }
  }
}
